﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Compiler_Phase_3
{
    public class Mnemonic
    {
        #region FIELDS
        int tokenCode;                                  //Associated Token Code
        string mnemonicStr;                //Mnemonic Code (up to 4 characters)
        string fullMnem;           //Full string of what 'mnemonicStr' is abbr.
        #endregion

        /*--------------------------------------------------------------------|
         * Constructor: Intializes the three fields to parameters given       |
         * ------------------------------------------------------------------*/
        public Mnemonic(int tok, string m, string f)
        {
            tokenCode = tok;
            mnemonicStr = m;
            fullMnem = f;
        }
        /*--------------------------------------------------------------------|
         * Getter Methods: Provides access to return field values.            |
         * ------------------------------------------------------------------*/
        #region GETTER METHODS
        public string MnemonicAccess
        {
            get
            {
                return mnemonicStr;
            }
        }
        public int TokenCode
        {
            get
            {
                return tokenCode;
            }
        }
        public string FullMnemonic
        {
            get
            {
                return fullMnem;
            }
        }
        #endregion
    }
    //-----------------------------------------------------------------------------
    //-----------------------------------------------------------------------------
    public class MnemonicTable
    {
        Mnemonic[] mTable;                 //Table holding all mnemonic strings
        int currentIndex;  //Holds the first currently open index for insertion
        const int MAX = 59;   //Holds maximum number of slots held in the table

        public MnemonicTable()
        {
            mTable = new Mnemonic[MAX];
        }
        /* -------------------------------------------------------------------|
         * Add Mnemonic takes an int (token code), string (mnemonic code),    |
         * string (what mnemonic abbreviates) to build a mnemonic table that  |
         * can later be accessed as needed.                                   |
         * ------------------------------------------------------------------*/
        public void AddMnemonic(int tCode, string mnem, string name)
        {
            mTable[currentIndex] = new Mnemonic(tCode, mnem, name);
            currentIndex++;
        }
        /*--------------------------------------------------------------------|
         * Get Mnemonic takes an int (tokenCode). It iterates through the     |
         * table looking for the token, and when found, returns the abbr      |
         * mnemonic code.                                                     |
         * ------------------------------------------------------------------*/
        public string GetMnemonic(int tokCode)
        {
            int mnem = 60;                     //Initiate to out of range value
            int i = 0;                               //Array iteration variable
            while (i < MAX && mnem == 60)    //While in array and code not found
            {
                if (tokCode == mTable[i].TokenCode)       //If token code found
                {
                    mnem = i;      //Set mnem to the index where mnemonic found
                }
                else                           //If mnemonic has not been found
                {
                    i++;                                 //Go to the next index
                }
            }
            return mTable[mnem].MnemonicAccess;           //return the mnemonic
        }
        /*--------------------------------------------------------------------|
         * Display prints the entire Mnemonic table in a clear format.        |
         * ------------------------------------------------------------------*/
        public void Display()
        {
            Console.WriteLine("Printing Mnemonic Table. Displaying Token" +
                "Code, Short Mnemonic and Full Mnemonic");  //Print description
            Console.WriteLine("Code\tShort\tFull");         //Print header line
            for (int i = 0; i < 56; i++)                //Iterate through table
            {                                                    //And print it
                Console.WriteLine(mTable[i].TokenCode + "\t" +
                    mTable[i].MnemonicAccess + "\t" + mTable[i].FullMnemonic);
            }
        }

    }
}
